package com.shanghaionstar.mro.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.shanghaionstar.R;
import com.shanghaionstar.mro.bean.violation.TrafficViolationRecord;

import java.util.List;

public class MroViolationAdapter extends BaseAdapter {
    private Context context;
    private List<TrafficViolationRecord> item;

    public MroViolationAdapter(Context context, List<TrafficViolationRecord> items) {
        this.context = context;
        this.item = items;
    }

    @Override
    public int getCount() {
        return item.size();
    }

    @Override
    public Object getItem(int position) {
        return item.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.mro_fragment_violation_item,
                    null);
            viewHolder.tv_violation_address = (TextView) convertView.findViewById(R.id.tv_violation_address);
            viewHolder.tv_violation_tong = (TextView) convertView.findViewById(R.id.tv_violation_tong);
            viewHolder.tv_violation_time = (TextView) convertView.findViewById(R.id.tv_violation_time);
            viewHolder.tv_point_negative = (TextView) convertView.findViewById(R.id.tv_point_negative);
            viewHolder.tv_point_one = (TextView) convertView.findViewById(R.id.tv_point_one);
            viewHolder.tv_point_ten = (TextView) convertView.findViewById(R.id.tv_point_ten);
            viewHolder.tv_fine_thousand = (TextView) convertView.findViewById(R.id.tv_fine_thousand);
            viewHolder.tv_fine_hundred = (TextView) convertView.findViewById(R.id.tv_fine_hundred);
            viewHolder.tv_fine_ten = (TextView) convertView.findViewById(R.id.tv_fine_ten);
            viewHolder.tv_fine_one = (TextView) convertView.findViewById(R.id.tv_fine_one);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_violation_time.setText(item.get(position).getTime());
        viewHolder.tv_violation_address.setText(item.get(position).getAddress());
        viewHolder.tv_violation_tong.setText(item.get(position).getContent());
        if (CommonUtil.isNull(item.get(position).getScore())) {
            viewHolder.tv_point_negative.setVisibility(View.INVISIBLE);
            viewHolder.tv_point_one.setBackgroundResource(R.drawable.mro_violation_zro);
        } else {
            viewHolder.tv_point_negative.setVisibility(View.VISIBLE);
            GetPointNumber(item.get(position).getScore(), viewHolder);
        }
        if (!CommonUtil.isNull(item.get(position).getPrice())) {
            GetFineNumber(item.get(position).getPrice(), viewHolder);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView tv_violation_time, tv_violation_address, tv_violation_tong, tv_point_negative, tv_point_one,
                tv_point_ten;
        private TextView tv_fine_thousand, tv_fine_hundred, tv_fine_ten, tv_fine_one;

    }

    /**
     * 截取罚款每位字符
     *
     * @param fine
     * @param viewHolder
     */
    public void GetFineNumber(String fine, ViewHolder viewHolder) {
        String fineNumber = fine;
        if (fine.contains(".")) {
            fineNumber = fine.substring(0, fine.lastIndexOf("."));
        }
        int number = Integer.valueOf(fineNumber);
        int one, ten, hundred, thousand;
        switch (fineNumber.length()) {
            case 1:
                one = number;
                SetBackground(one, viewHolder.tv_fine_one);
                viewHolder.tv_fine_one.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_ten.setVisibility(View.GONE);
                viewHolder.tv_fine_hundred.setVisibility(View.GONE);
                viewHolder.tv_fine_thousand.setVisibility(View.GONE);
                break;
            case 2:
                one = number % 10;
                SetBackground(one, viewHolder.tv_fine_one);
                ten = number / 10;
                SetBackground(ten, viewHolder.tv_fine_ten);
                viewHolder.tv_fine_one.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_ten.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_hundred.setVisibility(View.GONE);
                viewHolder.tv_fine_thousand.setVisibility(View.GONE);
                break;
            case 3:
                one = number % 100;
                SetBackground(one, viewHolder.tv_fine_one);
                ten = number / 10 % 10;
                SetBackground(ten, viewHolder.tv_fine_ten);
                hundred = number / 100;
                SetBackground(hundred, viewHolder.tv_fine_hundred);
                viewHolder.tv_fine_one.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_ten.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_hundred.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_thousand.setVisibility(View.GONE);
                break;
            case 4:
                one = number % 1000;
                SetBackground(one, viewHolder.tv_fine_one);
                ten = number / 10 % 100;
                SetBackground(ten, viewHolder.tv_fine_ten);
                hundred = number / 100 % 10;
                SetBackground(hundred, viewHolder.tv_fine_hundred);
                thousand = number / 1000;
                SetBackground(thousand, viewHolder.tv_fine_thousand);
                viewHolder.tv_fine_one.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_ten.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_hundred.setVisibility(View.VISIBLE);
                viewHolder.tv_fine_thousand.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 截取扣分
     *
     * @param point
     * @param viewHolder
     */
    public void GetPointNumber(String point, ViewHolder viewHolder) {
        int number = Integer.valueOf(point);
        int one, ten;
        switch (point.length()) {
            case 1:
                one = number;
                SetBackground(one, viewHolder.tv_point_one);
                break;
            case 2:
                one = number % 10;
                SetBackground(one, viewHolder.tv_point_one);
                ten = number / 10;
                SetBackground(ten, viewHolder.tv_point_ten);
                break;
        }
    }

    /**
     * 设置背景图片
     *
     * @param number
     * @param textView
     */
    public void SetBackground(int number, TextView textView) {
        switch (number) {
            case 0:
                textView.setBackgroundResource(R.drawable.mro_violation_zro);
                break;
            case 1:
                textView.setBackgroundResource(R.drawable.mro_violation_one);
                break;
            case 2:
                textView.setBackgroundResource(R.drawable.mro_violation_two);
                break;
            case 3:
                textView.setBackgroundResource(R.drawable.mro_violation_three);
                break;
            case 4:
                textView.setBackgroundResource(R.drawable.mro_violation_four);
                break;
            case 5:
                textView.setBackgroundResource(R.drawable.mro_violation_five);
                break;
            case 6:
                textView.setBackgroundResource(R.drawable.mro_violation_six);
                break;
            case 7:
                textView.setBackgroundResource(R.drawable.mro_violation_seven);
                break;
            case 8:
                textView.setBackgroundResource(R.drawable.mro_violation_eight);
                break;
            case 9:
                textView.setBackgroundResource(R.drawable.mro_violation_nine);
                break;
        }
    }
}
